package com.charlie.meetingmanagement.controller;

import com.charlie.meetingmanagement.pojo.dto.UpdateRoleRequest;
import com.charlie.meetingmanagement.pojo.po.PageBean;
import com.charlie.meetingmanagement.pojo.po.Result;
import com.charlie.meetingmanagement.pojo.po.User;
import com.charlie.meetingmanagement.pojo.vo.UserVo;
import com.charlie.meetingmanagement.requests.User_updataPassword_request;
import com.charlie.meetingmanagement.service.UserService;
import com.charlie.meetingmanagement.utils.JwtUtil;
import com.charlie.meetingmanagement.utils.MapstructUtil;

import io.swagger.v3.oas.annotations.responses.ApiResponse;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Random;

@Slf4j
@RestController
@CrossOrigin(origins = "*")
@ApiResponse()
public class UserController {

    @Resource
    private UserService userService;


//    查询员工的全部数据
    @PostMapping("/user/userInfo")
    public Result userInfo() {
        log.info("查询全部员工的数据");
        List<User> userList = userService.select();
        return Result.success(userList);
    }

//    条件查询user数据
    @GetMapping("/user/userInfoMore")
    public Result page(@RequestParam(defaultValue = "1") Integer page,
                       @RequestParam(defaultValue = "10") Integer pageSize,
                       @RequestParam(required = false) Integer userId,
                       @RequestParam(required = false) String userName,
                       @RequestParam(required = false) Integer userAge,
                       @RequestParam(required = false) String userTel,
                       @RequestParam(required = false) String userEmail,
                       @RequestParam(required = false) Integer userRoleA,
                       @RequestParam(required = false) Integer userRoleB,
                       @RequestParam(required = false) Integer userRoleC,
                       @RequestParam(required = false) Integer userSuperAdmin)
    {
        log.info("分页查询,参数:{},{},{},{},{},{},{},{},{},{},{}",page,pageSize,userId,userName,userAge,userTel,userEmail,
                userRoleA,userRoleC,userSuperAdmin,userRoleB);
        PageBean pageBean = userService.page(page,pageSize,userId,userName,userAge,userTel,userEmail,
                userRoleA,userRoleC,userSuperAdmin,userRoleB);
        return Result.success(pageBean);
    }

    //注册时默认他们的管理员相关权限均为0
    @PostMapping("/user/register")
    public Result register(@RequestBody User user) {
        if (user.getUserId()==null) {
            Random random = new Random();
            int randomNumber = 1000000 + random.nextInt(9000000);
            System.out.println(randomNumber);
            user.setUserId(randomNumber);
        }
        System.out.println(user);
        log.info("用户注册");
        return userService.register(user);
    }

//    通过id进行user数据更新
    @PostMapping("/user/update")
    public Result update(@RequestBody User user) {
        System.out.println(user);
        log.info("用户进行数据更新,更新id:{}", user.getUserId());
        return userService.update(user);
    }
    // 前台  通过id进行user  更新  用户密码
    @PostMapping("/user/updatePassword")
    public Result updatePassword(@RequestBody User_updataPassword_request request) {
        Integer userId = request.getUserId();
        String newPassword = request.getNewPassword();
        String oldPassword = request.getOldPassword();
        if (userService.updatePassword(userId,oldPassword,newPassword)){
            return Result.success();
        }
        return Result.error("Old password is error");
    }
//    通过id进行数据删除
    @GetMapping("/user/delete")
    public Result delete(@RequestParam Integer userId) {
        log.info("用户进行数据删除，删除id:{}",userId);
        return userService.delete(userId);
    }
    //前台通过用户账号获取用户信息
    @PostMapping("/user/getByUsername")
    public Result getByUsername(@RequestParam String userName,
                                @RequestParam String userPassword
    ){
        User user=userService.getByUsername(userName);
        if (user==null||!user.getUserPassword().equals(userPassword)){//当账号不存在或者密码错误
            return Result.error("This account is not exist");
        }else { //账号密码正确，观察这个用户是否被禁用
            if (user.getIsBanned()==0)
            {
                UserVo userVo = MapstructUtil.INSTANCT.User_PoToVo(user);

                Map<String,Object> claims = new HashMap<>();
                claims.put("userId",user.getUserId());
                String token = JwtUtil.getToken(claims);
                userVo.setToken(token);
                return Result.success(userVo);
            }
            return Result.error("This account is banned");
        }
    }
//    管理员登录
    @PostMapping("/adminLogin")
    public Result adminLogin(@RequestParam Integer userId,
                             @RequestParam String userPassword) {
//        根据用户ID和密码是否有数据进行判断
        log.info("adminLogin!!!!");
        if (userService.adminLogin(userId,userPassword)){
            Map<String,Object> claims = new HashMap<>();
            claims.put("userId",userId);
            String token = JwtUtil.getToken(claims);
            return Result.success(token);
        }
        return Result.success();
    }

//    封号与解封
    @PostMapping("/user/banById")
    public Result banById(@RequestParam Integer userId,@RequestParam Integer isBanned) {
        log.info("ban!!");
        return userService.banById(userId,isBanned);
    }

//    通过ID拿到一个人的全部信息
    @GetMapping("/user/getById")
    public Result getById(@RequestParam Integer userId) {
        log.info("getAllByID");
        return userService.getById(userId);
    }

//  更新权限
    @PostMapping("/user/updateRole")
    public Result updateRole(@RequestBody UpdateRoleRequest updateRoleRequest) {
        log.info("updateRole");
        return userService.updateRole(updateRoleRequest);
    }
}
